package cn.itsource.zcn.user.controller;

import cn.itsource.zcn.user.domain.Logininfo;
import cn.itsource.zcn.user.domain.User;
import cn.itsource.zcn.user.domain.UserDto;
import cn.itsource.zcn.user.service.ILogininfoService;
import cn.itsource.zcn.user.service.IUserService;
import cn.itsource.zcn.utils.AjaxResult;
import cn.itsource.zcn.utils.MD5Utils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;


/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2021-08-19 17:18:44
 */
@RestController
@RequestMapping("/user")
public class UserController {
    /**
     * 服务对象
     */
    @Autowired
    private IUserService tUserService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ILogininfoService logininfoService;

    /**
     * 查询全部数据
     *
     * @return List<>
     */
    @RequestMapping("/all")
    public List<User> queryAll() {
        return tUserService.queryAll();
    }

    /**
     * 登录
     * @return
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody Logininfo logininfo) {
        try {
            //根据前端数据查询数据库
            Logininfo logininfo1 = tUserService.login(logininfo);
            //如果数据库没有那个对象，表示没有该用户
            if(logininfo1==null){
                return new AjaxResult("用户名或密码错误");
            }
            logininfo1.setSalt("");
            logininfo1.setPassword("");
            String token = UUID.randomUUID().toString();
            // 存入redis中，token，和对象   与之拦截器对比
            redisTemplate.opsForValue().set(token, logininfo1, 30, TimeUnit.MINUTES);
            // 将信息发送给前端。前端保存到本地储存中
            HashMap<String, Object> map = new HashMap<>();
            map.put("token", token);
            map.put("logininfo", logininfo1);
            return AjaxResult.me().setResultObj(map);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }
    @PostMapping("/register")
    public AjaxResult register(@RequestBody UserDto userDto){
        // 验证码对比,比较
        String code = (String) redisTemplate.opsForValue().get(userDto.getPhone());
        code=code.split(":")[0];
        if(!code.equals(userDto.getCode())){
            return AjaxResult.me().setSuccess(false).setMessage("验证码错误");
        }
        // 密码加密处理
        String uuid = UUID.randomUUID().toString();
        User user = new User();
        user.setPhone(userDto.getPhone());
        user.setCreatetime(new Date());
        user.setSalt(uuid);
        String pwd= MD5Utils.encrypByMd5(userDto.getPassword()+uuid);
//        user.setPassword(userDto.getPassword());
        user.setPassword(pwd);
        user.setState(1);
        // 保存到数据库
        tUserService.save(user);

        Logininfo logininfo = new Logininfo();
        //设置值
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setDisable(1);
        logininfo.setUid(user.getId());

        logininfoService.save(logininfo);
        return AjaxResult.me();
    }

}
